<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-03 23:02:47
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_bookmarks extends discuz_table
{
    public function __construct()
    {
        $this->_table         = 'jamesonread_bookmarks';
        $this->_pk            = 'id';
        $this->_pre_cache_key = 'jamesonreadbookmarks_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    // 查询帖子是否已加入书签
    public function hasMark($tid, $uid)
    {
        return DB::result_first('SELECT id FROM %t WHERE tid=%d AND uid=%d ', array($this->_table, $tid, $uid));
    }
    // 获得用户本书的所有标签
    public function fetch_bybid($uid, $book_id)
    {
        $cacheid = 'fetchbybid-'.$uid.'-'.$book_id;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = (array) DB::fetch_all('SELECT b.id,b.tid,c.subject,c.pid FROM %t as b INNER JOIN %t as c ON b.tid=c.tid WHERE b.uid=%d AND b.book_id=%d ORDER BY c.zhangjie', array($this->_table, 'jamesonread_colums', $uid, $book_id));
        $this->store_cache($cacheid, $data);
        return $data;
    }
    // 获取当前用户所有书签
    public function fetch_by_uid($uid, $size = 0,$start = 0)
    {
        $limit = '';
        if($start){
            $limit = ' LIMIT  '.$start.','.$size;
        }else if($size){
            $limit = ' LIMIT '.$size;
        }
        $data = DB::fetch_all('SELECT m.hash,m.id,m.uid,m.book_id,m.tid,b.book_name,c.subject,c.pid FROM %t AS m INNER JOIN %t AS b ON m.book_id=b.book_id INNER JOIN %t AS c ON m.tid=c.tid WHERE m.uid=%d ORDER BY  m.addtime DESC ' .$limit, array($this->_table, 'jamesonread_books', 'jamesonread_colums', $uid));
        return $data;
    }
    public function count_by_uid($uid){
        return DB::result_first('SELECT count(id) FROM %t WHERE uid=%d',array($this->_table,$uid));
    }
}